#include "event/loop.hpp"
#include "fmt/core.h"
#include "network/sockaddr.hpp"
#include "network/tcp_server.hpp"

using namespace std;
using namespace ibox::event;
using namespace ibox::network;
int main() {
    SockAddr bind_addr = SockAddr::from_string("127.0.0.1:8080");
    auto loop = Loop();

    TcpServer server{&loop};
    server.initialize(bind_addr, 1);
    server.set_receive_callback(
        // TODO: 回调函数设计为不需要直接访问server对象感觉会好一些
        // server.send(conn, ...) ==> conn->send(...)
        [&](const ConnToken *conn, Buffer &buf) {
            //expect(server.send(*conn, buf.readable_begin(), buf.readable_size()));
            server.send(conn, buf.readable_begin(), buf.readable_size());
            buf.has_readall();
        },
        0);
    server.start();

    if (bind_addr.type() == SockAddr::Type::IPv4) {
        IpAddress ip;
        uint16_t port = 0;
        bind_addr.get(ip, port);
        cout << "try command: nc " << ip.to_string() << ' ' << port << '\n';
    }
    loop.run();
}
